/**
 * @Author：ming
 * @Date：2023/10/3 1:53 PM
 * @Filename：Lc684
 */

import java.util.ArrayList;
import java.util.HashSet;

/**
 * @author ming
 */
public class Lc684 {
    public int[] findRedundantConnection(int[][] edges){
        int[] parent = new int[edges.length];

        for (int i = 0; i < parent.length; i++) {
            parent[i] = i;
        }

        for (int i = 0; i < edges.length; i++) {
            int set1= findParent(edges[i][0],parent);
            int set2= findParent(edges[i][1],parent);
            if(set1==set2){
                return edges[i];
            }else{
                parent[set1] = set2;
            }

        }



        return new int[]{0,0};
    }

    public static int findParent(int n,int[] parent){
        int sum = n;
        while (parent[sum]!=sum){
            sum = parent[sum];
        }
        return sum;
    }

}
